package net.hubbery.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import net.hubbery.bean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {
    //操作数据库
//    public boolean checkUser(String name){
//        ComboPooledDataSource dataSource = new ComboPooledDataSource();
//        QueryRunner queryRunner = new QueryRunner(dataSource);
//        String sql = "select name from user where name = ?";
//        try {
//            User user = queryRunner.query(sql,new BeanHandler<User>(User.class),name);
//            if (user == null ){
//                return true;
//            }else {
//                return false;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }
//
//    public boolean register(User user) {
//        ComboPooledDataSource dataSource = new ComboPooledDataSource();
//        QueryRunner queryRunner = new QueryRunner(dataSource);
//        String sql = "insert into user values(null,?,?,?)";
//        try {
//            int row = queryRunner.update(sql, user.getName(), user.getPassword(), user.getEmail());
//            System.out.println(row);
//            if(row > 0){
//                return true;
//            }else {
//                return false;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        }
//    }

    public boolean login(String account, String password) {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);
        String sql;
        sql = "select * from user where account = ? and password = ?";
        User user = null;
        try {
            user = queryRunner.query(sql,new BeanHandler<User>(User.class),account,password);

            if(user != null){
                return true;
            }else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
